package com.jhhc.StormSurgeForecast.AstronomicalTide.ServiceImpl;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.BDMS_FBC_TFINFO_BMapper;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFINFO_B;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.TyphoonInfo;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFINFO_BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LYQ
 */
@Service("BDMS_FBC_TFINFO_BService")
@Transactional
public class BDMS_FBC_TFINFO_BServiceImpl implements BDMS_FBC_TFINFO_BService {

    @Autowired
    private BDMS_FBC_TFINFO_BMapper mapper;

    @Override
    public BDMS_FBC_TFINFO_B queryById(String tfid) {
        return mapper.queryById(tfid);
    }

    @Override
    public List<TyphoonInfo> getInfos(String year) {
        List<BDMS_FBC_TFINFO_B> bList = mapper.getInfos(year);
        if (bList != null && bList.size() > 0) {
            bList.sort(Comparator.comparing(BDMS_FBC_TFINFO_B::getTfid));
            Collections.reverse(bList);
            List<TyphoonInfo> typhoonInfos = new ArrayList<>();
            TyphoonInfo typhoonInfo = null;
            for (BDMS_FBC_TFINFO_B bdmsFbcTfinfoB : bList) {
                typhoonInfo = new TyphoonInfo();
                if (bdmsFbcTfinfoB.getTfid() != null && !"".equals(bdmsFbcTfinfoB.getTfid())) {
                    typhoonInfo.setTyphoonId(bdmsFbcTfinfoB.getTfid());
                }
                if (bdmsFbcTfinfoB.getTfnameC() != null && !"".equals(bdmsFbcTfinfoB.getTfnameC())) {
                    typhoonInfo.setTyphoonName(bdmsFbcTfinfoB.getTfnameC());
                }
                if (bdmsFbcTfinfoB.getTfnameE() != null && !"".equals(bdmsFbcTfinfoB.getTfnameE())) {
                    typhoonInfo.setTyphoonNameE(bdmsFbcTfinfoB.getTfnameE());
                }
                if (bdmsFbcTfinfoB.getEndtime() != null && !"".equals(bdmsFbcTfinfoB.getEndtime())) {
                    typhoonInfo.setEndTime(bdmsFbcTfinfoB.getEndtime());
                }
                if (bdmsFbcTfinfoB.getLand() != null) {
                    typhoonInfo.setLand(bdmsFbcTfinfoB.getLand() != 1);
                }
                if (bdmsFbcTfinfoB.getCountry() != null && !"".equals(bdmsFbcTfinfoB.getCountry())) {
                    typhoonInfo.setCountry(bdmsFbcTfinfoB.getCountry());
                }
                if (bdmsFbcTfinfoB.getProvince() != null && !"".equals(bdmsFbcTfinfoB.getProvince())) {
                    typhoonInfo.setProvice(bdmsFbcTfinfoB.getProvince());
                }
                if (bdmsFbcTfinfoB.getCity() != null && !"".equals(bdmsFbcTfinfoB.getCity())) {
                    typhoonInfo.setCity(bdmsFbcTfinfoB.getCity());
                }
                if (bdmsFbcTfinfoB.getArea() != null && !"".equals(bdmsFbcTfinfoB.getArea())) {
                    typhoonInfo.setArea(bdmsFbcTfinfoB.getArea());
                }
                if (bdmsFbcTfinfoB.getEffectareas() != null && !"".equals(bdmsFbcTfinfoB.getEffectareas())) {
                    typhoonInfo.setEffectArea(bdmsFbcTfinfoB.getEffectareas());
                }
                typhoonInfo.setStartTime(bdmsFbcTfinfoB.getStarttime());
                typhoonInfos.add(typhoonInfo);
            }
            return typhoonInfos;
        }else {
            return null;
        }
    }

    @Override
    public List<String> getYears() {
        List<String> strings = mapper.getYears();
        if (strings != null && strings.size() > 0) {
            return strings.stream().distinct().collect(Collectors.toList());
        }else {
            return null;
        }
    }

    @Override
    public List<TyphoonInfo> getOccurent(String year) {
        List<BDMS_FBC_TFINFO_B> bList = mapper.getOccurent(year);
        if (bList != null && bList.size() > 0) {
            List<TyphoonInfo> typhoonInfos = new ArrayList<>();
            TyphoonInfo typhoonInfo = null;
            for (BDMS_FBC_TFINFO_B bdmsFbcTfinfoB : bList) {
                typhoonInfo = new TyphoonInfo();
                if (bdmsFbcTfinfoB.getTfid() != null && !"".equals(bdmsFbcTfinfoB.getTfid())) {
                    typhoonInfo.setTyphoonId(bdmsFbcTfinfoB.getTfid());
                }
                if (bdmsFbcTfinfoB.getTfnameC() != null && !"".equals(bdmsFbcTfinfoB.getTfnameC())) {
                    typhoonInfo.setTyphoonName(bdmsFbcTfinfoB.getTfnameC());
                }
                if (bdmsFbcTfinfoB.getTfnameE() != null && !"".equals(bdmsFbcTfinfoB.getTfnameE())) {
                    typhoonInfo.setTyphoonNameE(bdmsFbcTfinfoB.getTfnameE());
                }
                if (bdmsFbcTfinfoB.getEndtime() != null && !"".equals(bdmsFbcTfinfoB.getEndtime())) {
                    typhoonInfo.setEndTime(bdmsFbcTfinfoB.getEndtime());
                }
                if (bdmsFbcTfinfoB.getLand() != null) {
                    typhoonInfo.setLand(bdmsFbcTfinfoB.getLand() != 1);
                }
                if (bdmsFbcTfinfoB.getCountry() != null && !"".equals(bdmsFbcTfinfoB.getCountry())) {
                    typhoonInfo.setCountry(bdmsFbcTfinfoB.getCountry());
                }
                if (bdmsFbcTfinfoB.getProvince() != null && !"".equals(bdmsFbcTfinfoB.getProvince())) {
                    typhoonInfo.setProvice(bdmsFbcTfinfoB.getProvince());
                }
                if (bdmsFbcTfinfoB.getCity() != null && !"".equals(bdmsFbcTfinfoB.getCity())) {
                    typhoonInfo.setCity(bdmsFbcTfinfoB.getCity());
                }
                if (bdmsFbcTfinfoB.getArea() != null && !"".equals(bdmsFbcTfinfoB.getArea())) {
                    typhoonInfo.setArea(bdmsFbcTfinfoB.getArea());
                }
                if (bdmsFbcTfinfoB.getEffectareas() != null && !"".equals(bdmsFbcTfinfoB.getEffectareas())) {
                    typhoonInfo.setEffectArea(bdmsFbcTfinfoB.getEffectareas());
                }
                typhoonInfo.setStartTime(bdmsFbcTfinfoB.getStarttime());
                typhoonInfos.add(typhoonInfo);
            }
            typhoonInfo = null;
            return typhoonInfos;
        }else {
            return null;
        }
    }
}
